package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.mapper.UserInfoMapper;
import com.tanhua.pojo.TodayBest;
import com.tanhua.pojo.User;
import com.tanhua.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class RecommendUserService {

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;


    public PageResult queryRecommendation(String token, RecommendUserQueryParam recommendUserQueryParam) {

        User user = userService.queryUserByToken(token);

        if (null == user){
            return null;
        }

        PageResult pageResult = new PageResult();
        pageResult.setPage(recommendUserQueryParam.getPage());
        pageResult.setPagesize(recommendUserQueryParam.getPagesize());

        PageInfo<RecommendUser> recommendUserPageInfo = recommendUserApi.queryPageInfo(user.getId(), recommendUserQueryParam.getPage(), recommendUserQueryParam.getPagesize());
//        PageInfo<RecommendUser> recommendUserPageInfo = recommendUserApi.queryPageInfo(1L, recommendUserQueryParam.getPage(), recommendUserQueryParam.getPagesize());//测试

        List<RecommendUser> recommendUsers = recommendUserPageInfo.getRecords();
        if (recommendUsers.isEmpty()){
            //没有推荐用户
            return pageResult;
        }

        //有推荐用户, 按照条件查询, 将结果返回给前端, set集合去重
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser recommendUser : recommendUsers) {
            userIds.add(recommendUser.getUserId());
        }

        //查询
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getUserId,userIds);

        //解析前端传来的参数, 如果前端没有传对应筛选数据的话, 则获取值为空
        if (StringUtils.isEmpty(recommendUserQueryParam.getGender())) {
            String gender = recommendUserQueryParam.getGender();
            queryWrapper.eq(UserInfo::getSex,"man".equals(gender)?"1":"2");
        }
        //String lastLogin = recommendUserQueryParam.getLastLogin();
        if (null != recommendUserQueryParam.getAge()) {
            Integer age = recommendUserQueryParam.getAge();
            queryWrapper.lt(UserInfo::getAge,age);
        }

        if (StringUtils.isEmpty(recommendUserQueryParam.getCity())) {
            String city = recommendUserQueryParam.getCity();
            queryWrapper.like(UserInfo::getCity,city);
        }

        if (StringUtils.isEmpty(recommendUserQueryParam.getEducation())) {
            String education = recommendUserQueryParam.getEducation();
            queryWrapper.eq(UserInfo::getEdu,education);
        }

        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfos)){
            return pageResult;
        }

        //将用户信息列表中的数据封装到TodayBest Pojo中
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            TodayBest todayBest = new TodayBest();
            todayBest.setAge(userInfo.getAge());
            todayBest.setId(Long.valueOf(userInfo.getId()));

            for (RecommendUser recommendUser : recommendUsers) {
                if (recommendUser.getUserId() == userInfo.getId().longValue()){
                    todayBest.setFateValue(recommendUser.getScore().longValue());
                }
            }

//            todayBest.setGender(userInfo.getSex()==1?"man":"woman");
            todayBest.setGender(userInfo.getSex().getValue() == 1?"man":"woman");
            todayBest.setTags(userInfo.getTags().split(","));
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBests.add(todayBest);
        }

        Collections.sort(todayBests, new Comparator<TodayBest>() {
            @Override
            public int compare(TodayBest o1, TodayBest o2) {
                return new Long(o2.getFateValue()-o1.getFateValue()).intValue();
            }
        });
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
